<template>
  <div class="_wz_IncumbencyAdd">
    <Form ref="formAdd" :model="formData" label-position="right" :label-width="96" :rules="ruleValidate">
      <FormItem label="人员姓名" prop="name">
        <i-input v-model="formData.name" placeholder="请填写人员名称" clearable disabled/>
      </FormItem>
      <FormItem label="身份证号" prop="identifyNum">
        <i-input v-model="formData.identifyNum" placeholder="请填写身份证号" clearable disabled/>
      </FormItem>
      <FormItem label="从业类型" prop="type">
        <i-select v-model="formData.type" filterable placeholder="请选择从业类型" clearable disabled>
          <Option v-for="item in personType" :value="item.code" :key="item.code">{{item.value}}</Option>
        </i-select>
      </FormItem>
      <FormItem label="运输企业" prop="deptUuid">
        <i-select v-model="formData.deptUuid" filterable placeholder="请选择运输企业" clearable disabled>
          <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{item.deptName}}</Option>
        </i-select>
      </FormItem>
      <FormItem label="人员性别" prop="gender">
        <i-select v-model="formData.gender" filterable placeholder="请选择人员性别" clearable>
          <Option value="男">男</Option>
          <Option value="女">女</Option>
        </i-select>
      </FormItem>
      <FormItem label="联系电话" prop="phone">
        <i-input v-model="formData.phone" placeholder="请填写联系方式" clearable/>
      </FormItem>
      <FormItem label="出生年月" prop="wzDateOfBirth">
        <DatePicker type="date" v-model="formData.wzDateOfBirth" placeholder="请选择出生年月"></DatePicker>
      </FormItem>
      <FormItem label="准驾车型" prop="drivingType">
        <i-select v-model="formData.drivingType" filterable placeholder="请选择准驾车型" clearable>
          <Option v-for="item in drivingType" :value="item.code" :key="item.code">{{item.value}}</Option>
        </i-select>
      </FormItem>
      <FormItem label="文化水平" prop="educationLevel">
        <i-select v-model="formData.educationLevel" filterable placeholder="请选择文化水平" clearable>
          <Option v-for="item in educationLevel" :value="item.value" :key="item.code">{{item.value}}</Option>
        </i-select>
      </FormItem>
      <FormItem label="归属部门" prop="inOrgUuid">
        <i-select v-model="formData.inOrgUuid" placeholder="请选择归属部门" clearable>
          <Option v-for="item in departmentList" :value="item.uuid" :key="item.uuid">{{ item.inOrgName }}</Option>
        </i-select>
      </FormItem>
      <FormItem label="驾乘车辆" prop="vehiclePlate">
        <i-input v-model="formData.vehiclePlate" placeholder="请填写驾乘车辆" clearable />
      </FormItem>
      <FormItem label="电子邮箱" prop="email">
        <i-input v-model="formData.email" placeholder="请填写电子邮箱" clearable />
      </FormItem>
      <FormItem label="联系地址" prop="address">
        <i-input v-model="formData.address" placeholder="请填写联系地址" clearable />
      </FormItem>

      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="$emit('closeFun', false)">取消</Button>
        <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
      </div>
    </Form>
    <Spin size="large" fix v-if="spinShow"></Spin>
  </div>
</template>
<script>
  export default {
    props: ["selectData"],
    data() {
      return {
        spinShow:false,//本页加载
        subDeptList: [], //下级企业列表
        personType: [], //从业类型
        educationLevel: [], //文化程度
        drivingType: [], //准驾车型
        departmentList:[],//部门
        formData: {},
        ruleValidate: {
          gender: [
            {required: true, message: '请选择人员性别', trigger: 'blur'}
          ],
          educationLevel: [
            {required: true, message: '请选择文化水平', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请填写联系方式', trigger: 'blur'},
            {
              validator: (rule, value, callBack) => {
                if (/^[1][0-9]{10}$/.test(value) || /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(value)) {
                  callBack()
                } else {
                  callBack('请输入手机号 或者 区号-座机号')
                }
              }, trigger: 'blur'
            }
          ],
          // vehiclePlate: [
          //   {required: true, message: '请填写车辆牌号', trigger: 'blur'}
          // ],
          drivingType: [
            {required: true, message: '请选择准驾车型', trigger: 'blur'}
          ],
          wzDateOfBirth: [
            {type:'date',required: true, message: '请选择出生年月', trigger: 'blur'}
          ],
        }
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态

    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      let that = this;
      // 0 是 添加 1是修改
        that.formData = that.selectData;
      that.$set(that.formData, 'wzDateOfBirth', new Date(that.formData.dateOfBirth))//赋值


      //从业类型
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'person_type'}).then(res => {
        if (!!res) {
          that.personType = res.data.data;
        }
      }).catch(err => {
        console.log('失败', err);
      })


      //文化程度
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'education_level'}).then(res => {
        if (!!res) {
          that.educationLevel = res.data.data;
        }
      }).catch(err => {
        console.log('失败', err);
      })

      //准驾车型
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'driving_type'}).then(res => {
        if (!!res) {
          that.drivingType = res.data.data;
        }
      }).catch(err => {
        console.log('失败', err);
      })



      //运输企业
      that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
          that.$set(that.formData, 'deptUuid', that.subDeptList[0].uuid)//赋值

          //部门级联
          that.enterpriseGroup()
        }
      }).catch(err => {
        console.log("失败", err)
      })

    },
    methods: {//执行的方法
      //部门级联
      enterpriseGroup(){
        let that = this;
        //部门
        that.axios.post(that.apiUrl.coreInOrgCoreInOrgList,{deptUuid:that.formData.deptUuid,inOrgType:2}).then(res => {
          if (!!res) {
            that.departmentList = res.data.data;
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
      //提交表单
      addSubmit(data) {
        let that = this;
        that.$refs[data].validate((valid) => {
          if (valid) {//正则判断使用

            that.formData.dateOfBirth = that.public.timeFormatting(that.formData.wzDateOfBirth, "yyyy-MM-dd")
            that.spinShow = true;//显示加载
            that.axios.post(that.apiUrl.coreDriverCoreDriverSaveOrUpdate, that.formData).then(res => {
              that.spinShow = false;
              if (!!res) {
                that.$Message.success("操作成功");
                that.$emit("closeFun", true)
              }
            }).catch(err => {
              console.log('失败', err);
            })
          } else {
            that.$Message.error('请正确填写表单');
          }
        })
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_IncumbencyAdd {


  }
</style>
